//
//  DHSafeAccountControntroller.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/16.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHSafeAccountControntroller.h"
#import "JPUserAccountMember.h"
#import "DHPayPwdController.h"
@interface DHSafeAccountControntroller ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeServiceConsTop;

@property (weak, nonatomic) IBOutlet UILabel *emailLabel;
@property(nonatomic ,strong)JPUserAccountMember *memberModel;
@property (weak, nonatomic) IBOutlet UILabel *moblieLabel;
@property (weak, nonatomic) IBOutlet UILabel *pwdLabel;
@property (weak, nonatomic) IBOutlet UIButton *emailButton;
@property (weak, nonatomic) IBOutlet UIButton *phoneButton;

@property(nonatomic ,strong)NSArray *memberConfirms;

@end

@implementation DHSafeAccountControntroller

- (void)setMemberModel:(JPUserAccountMember *)memberModel
{
    _memberModel = memberModel;
    
    UIColor *bindColor = [UIColor colorWithRed:102.0f/255.0f green:177.0f/255.0f blue:246.0f/255.0f alpha:1];
    UIColor *noBindColor = [UIColor colorWithRed:238.0f/255.0f green:152.0f/255.0f blue:29.0f/255.0f alpha:1];
    self.emailLabel.text = memberModel.member_email_bind.boolValue ? @"已设置" : @"未设置" ;
    self.moblieLabel.text = memberModel.member_mobile_bind.boolValue ? @"已设置" : @"未设置" ;
    self.pwdLabel.text = memberModel.member_paypwd ? @"已设置" : @"未设置" ;
    
    self.emailLabel.textColor = memberModel.member_email_bind.boolValue ? bindColor : noBindColor;
    self.moblieLabel.textColor = memberModel.member_mobile_bind.boolValue ? bindColor : noBindColor;
    self.pwdLabel.textColor = memberModel.member_paypwd ? bindColor : noBindColor;
    NSString *emailTxt = memberModel.member_email_bind.boolValue ? @"修改邮箱" : @"绑定邮箱";
    NSString *phoneTxt = memberModel.member_mobile_bind.boolValue ? @"修改手机" : @"绑定手机";
    [self.emailButton setTitle:emailTxt forState:UIControlStateNormal];
    [self.phoneButton setTitle:phoneTxt forState:UIControlStateNormal];
    
    NSMutableArray *arrayM = [NSMutableArray array];
    if (memberModel.member_mobile_bind.boolValue) {[arrayM addObject:memberModel.member_mobile];}
    if (memberModel.member_email_bind.boolValue) {[arrayM addObject:memberModel.member_email];}
    
    self.memberConfirms = arrayM.copy;
}

#pragma mark: - 修改密码按钮监听
- (IBAction)btnChangePwdClick:(UIButton *)sender
{
    
    // 邮箱 手机没绑定 则跳转绑定 邮箱
    if (!(self.memberModel.member_email_bind.boolValue) &&
        !(self.memberModel.member_mobile_bind.boolValue))
    {
        [SVProgressHUD showInfoWithStatus:@"请先绑定邮箱"];
        [self performSegueWithIdentifier:@"confirmEmail" sender:nil];
    }
    
    [self performSegueWithIdentifier:@"changePwdID" sender:@"modify_pwd"];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isMemberOfClass:[DHPayPwdController class]]) {
        [(DHPayPwdController *)segue.destinationViewController setConfirmList:self.memberConfirms];
      
        [(DHPayPwdController *)segue.destinationViewController setType:sender];
    }
}
#pragma mark: - 支付密码按钮监听
- (IBAction)paybuttonClick:(UIButton *)sender
{
     [self performSegueWithIdentifier:@"changePwdID" sender:@"modify_paypwd"];
}
#pragma mark: - 绑定手机按钮监听
- (IBAction)phoneButtonClick:(UIButton *)sender
{
    // 没有绑定手机 跳转绑定界面
    if (!self.memberModel.member_mobile_bind.boolValue)
    {
        [self performSegueWithIdentifier:@"nobBndMoblieID" sender:nil];
        return;
    }
    [self performSegueWithIdentifier:@"changePwdID" sender:@"modify_mobile"];
}
#pragma mark: - 绑定邮箱按钮监听
- (IBAction)emailButtonClick:(UIButton *)sender
{
    // 没有绑定邮箱 跳转绑定界面
    if (!self.memberModel.member_email_bind.boolValue)
    {
        [self performSegueWithIdentifier:@"confirmEmail" sender:nil];
        return;
    }
    [self performSegueWithIdentifier:@"changePwdID" sender:@"modify_email"];
}

+ (instancetype)safeContronller
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"DHSafeAccountControntroller" bundle:nil];
    DHSafeAccountControntroller *vc = [sb instantiateViewControllerWithIdentifier:@"safeVCID"];
    return vc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadSafeInfo];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupNavBar];

}
#pragma mark: - 加载用户安全网络请求
- (void)loadSafeInfo
{
    NSString *url = [NSString stringWithFormat:@"%@%@",REMOTEURL,@"index.php?act=member_security&op=member"];
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[@"key"] = DHNetworkManager.shareManager.userAccount.key;
    [DHNetworkManager.shareManager requestWithMethod:MethodPost UrlString:url params:dictM.copy completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            [SVProgressHUD showInfoWithStatus:@"请检查网络"];
            return ;
        }
        
        id temp = json[@"datas"];
        if (temp[@"error"]) {
            [SVProgressHUD showInfoWithStatus:temp[@"error"]];
            return;
        }
        
        self.memberModel = [JPUserAccountMember yy_modelWithJSON:temp];
       
    }];
}
#pragma mark: - 设置导航栏
- (void)setupNavBar
{
    self.navigationController.navigationBar.hidden = false;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:222.0f/255.0f green:60.0f/255.0f blue:50.0f/255.0f alpha:1];
    self.title = @"账户安全";
    [self.navigationController.navigationBar setTintColor:UIColor.whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:17],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
    self.safeServiceConsTop.constant = CGRectGetMaxY(self.navigationController.navigationBar.frame) + 12;
    [self.view layoutIfNeeded];
}



@end
